local extension = Package("rfenghou_yan")
extension.extensionName = "aaa_fenghou"

local U = require "packages/utility/utility"
local DIY = require "packages/diy_utility/diy_utility"

Fk:loadTranslationTable{
  ["rfenghou_yan"] = "异燕",
}

local function AddWinAudio(general)
  local Win = fk.CreateActiveSkill{ name = general.name.."_win_audio" }
  Win.package = extension
  Fk:addSkill(Win)
end

local murongchui = General:new(extension, "rfenghou__murongchui", "yan", 4)
local tufua = fk.CreateTriggerSkill{
  name = "rfenghou__tufua",
  anim_type = "support",
  events = {fk.DamageInflicted, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.DamageInflicted then
        return true
      elseif event == fk.DamageCaused then
        return player:isWounded() or player.maxHp < player:getMark(self.name)
      end
    end
  end,
  on_cost  = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__tufua1-invoke")
    elseif event == fk.DamageCaused then
      local choices = {"Cancel"}
      if player.maxHp < player:getMark(self.name) then
        table.insert(choices, 1, "rfenghou__tufua_maxhp")
      end
      if player:isWounded() then
        table.insert(choices, 1, "recover")
      end
      local choice = player.room:askForChoice(player, choices, self.name,
        "#rfenghou__tufua2-invoke::"..data.to.id, false, {"recover", "rfenghou__tufua_maxhp", "Cancel"})
      if choice ~= "Cancel" then
        self.cost_data = {choice = choice}
        return true
      end
    end
  end,
  on_use  = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      room:changeMaxHp(player, -1)
    elseif event == fk.DamageCaused then
      if self.cost_data.choice == "recover" then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name,
        })
      else
        room:changeMaxHp(player, 1)
      end
    end
    return true
  end,

  refresh_events = {fk.MaxHpChanged},
  can_refresh = function (self, event, target, player, data)
    return target == player and player.maxHp > player:getMark(self.name)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, self.name, player.maxHp)  --体力上限上限，绷
  end,

  on_acquire = function(self, player)
    if player.maxHp > player:getMark(self.name) then
      player.room:setPlayerMark(player, self.name, player.maxHp)  --体力上限上限，绷
    end
  end,
}
local tufub = fk.CreateTriggerSkill{
  name = "rfenghou__tufub",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.MaxHpChanged, fk.HpRecover},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.MaxHpChanged then
        if player:getMark("rfenghou__tufub-tmp") > 0 then
          player.room:setPlayerMark(player, "rfenghou__tufub-tmp", 0)
          return not player:isWounded()
        end
      elseif event == fk.HpRecover then
        return not player:isWounded()
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.MaxHpChanged then
      local choices = {"rfenghou__tufub_attackrange"}
      if player:getHandcardNum() < player:getMaxCards() then
        table.insert(choices, "rfenghou__tufub_draw")
      end
      local choice = room:askForChoice(player, choices, self.name)
      if choice == "rfenghou__tufub_attackrange" then
        room:addPlayerMark(player, self.name, 1)
      else
        player:drawCards(player:getMaxCards() - player:getHandcardNum(), self.name)
      end
    elseif event == fk.HpRecover then
      local choices = {"rfenghou__tufub_maxcards"}
      local targets = table.filter(room:getOtherPlayers(player), function (p)
        return player:inMyAttackRange(p) and not p:isNude()
      end)
      if #targets > 0 then
        table.insert(choices, "rfenghou__tufub_prey")
      end
      local choice = room:askForChoice(player, choices, self.name)
      if choice == "rfenghou__tufub_maxcards" then
        room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
      else
        room:doIndicate(player.id, table.map(targets, Util.IdMapper))
        for _, p in ipairs(targets) do
          if player.dead then return end
          if not p.dead and not p:isNude() then
            local card = room:askForCardChosen(player, p, "he", self.name, "#rfenghou__tufub-prey::"..p.id)
            room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
          end
        end
      end
    end
  end,

  refresh_events = {fk.BeforeMaxHpChanged},  --FIXME: MaxHpChanged时机没继承BeforeMaxHpChanged时机的data……
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(self, true) and data.num < 0 and player:isWounded()
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rfenghou__tufub-tmp", 1)
  end,
}
local tufub_attackrange = fk.CreateAttackRangeSkill{
  name = "#rfenghou__tufub_attackrange",
  correct_func = function (self, from, to)
    return from:getMark("rfenghou__tufub")
  end,
}
local tufuc = fk.CreateTriggerSkill{
  name = "rfenghou__tufuc$",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.GameStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(player.room.alive_players, function (p)
      return p.kingdom == "yan"
    end)
    if #targets == 0 then return end
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    for _, p in ipairs(targets) do
      if not p.dead then
        if player.dead or p == player then
          room:damage{
            from = player,
            to = p,
            damage = 1,
            skillName = self.name,
          }
        else
          local choice = room:askForChoice(p, {"rfenghou__tufuc1:"..player.id, "rfenghou__tufuc2:"..player.id}, self.name)
          if choice[16] == 1 then
            room:damage{
              from = p,
              to = player,
              damage = 1,
              skillName = self.name,
            }
          else
            room:damage{
              from = player,
              to = p,
              damage = 1,
              skillName = self.name,
            }
          end
        end
      end
    end
  end,
}
tufub:addRelatedSkill(tufub_attackrange)
murongchui:addSkill(tufua)
murongchui:addSkill(tufub)
murongchui:addSkill(tufuc)
Fk:loadTranslationTable{
  ["rfenghou__murongchui"] = "慕容垂",
  ["#rfenghou__murongchui"] = "鹰庇苍苍",
  ["illustrator:rfenghou__murongchui"] = "乱世逐鹿",
  ["designer:rfenghou__murongchui"] = "zzcclll朱苦力",
  ["cv:rfenghou__murongchui"] = "柳川鱼",

  ["rfenghou__tufua"] = "途缚",
  [":rfenghou__tufua"] = "当你受到伤害时，你可以改为减1点体力上限；当你造成伤害时，你可以改为回复1点体力或体力上限。",
  ["rfenghou__tufub"] = "徒复",
  [":rfenghou__tufub"] = "锁定技，当你扣减体力上限至体力后，选择一项：攻击范围+1，或将手牌摸至手牌上限；当你回复体力至上限后，选择一项："..
  "手牌上限+1，或获得攻击范围内所有角色一张牌。",
  ["rfenghou__tufuc"] = "图附",
  [":rfenghou__tufuc"] = "主公技，锁定技，游戏开始时，燕势力角色依次选择一项：其对你造成1点伤害，或你对其造成1点伤害。",
  ["#rfenghou__tufua1-invoke"] = "途缚：是否减1点体力上限，防止你受到的伤害？",
  ["#rfenghou__tufua2-invoke"] = "途缚：是否选择一项，防止你对 %dest 造成的伤害？",
  ["rfenghou__tufua_maxhp"] = "加1点体力上限",
  ["rfenghou__tufub_attackrange"] = "攻击范围+1",
  ["rfenghou__tufub_draw"] = "将手牌摸至手牌上限",
  ["rfenghou__tufub_maxcards"] = "手牌上限+1",
  ["rfenghou__tufub_prey"] = "获得攻击范围内角色各一张牌",
  ["#rfenghou__tufub-prey"] = "徒复：获得 %dest 一张牌",
  ["rfenghou__tufuc1"] = "你对%src造成1点伤害",
  ["rfenghou__tufuc2"] = "%src对你造成1点伤害",

  ["$rfenghou__tufua1"] = "国士之礼每深，报德之分未一，善始无终。",
  ["$rfenghou__tufua2"] = "宿心难负，设使秦运必穷，关西之地，会非吾有。",
  ["$rfenghou__tufub1"] = "外假秦声，内规兴复，端拱可定东夏。",
  ["$rfenghou__tufub2"] = "天启嘉会、灵命暂迁，此鸿渐之始，龙变之初。",
  ["$rfenghou__tufuc1"] = "九仞之功，不让一篑，岂可怒而轻掷？",
  ["$rfenghou__tufuc2"] = "朝摩燕阙，暮谒秦关，古时未能有也。",
  ["~rfenghou__murongchui"] = "尸骨积山，亲者哀嚎，朕之过也，朕之过也……",
}

local murongyu = General:new(extension, "rfenghou__murongyu", "yan", 3, 3, General.Female)
local dianyi = fk.CreateActiveSkill{
  name = "rfenghou__dianyi",
  anim_type = "control",
  prompt = "#rfenghou__dianyi",
  card_num = 0,
  target_num = 0,
  derived_piles = "rfenghou__dianyi",
  interaction = function(self)
    local choices = {"draw1"}
    if table.find(Fk:currentRoom().alive_players, function (p)
      return p:getHandcardNum() == Self:getHandcardNum() and not p:isNude()
    end) then
      table.insert(choices, "rfenghou__dianyi_discard")
    end
    return UI.ComboBox { choices = choices, all_choices = {"draw1", "rfenghou__dianyi_discard"} }
  end,
  can_use = function(self, player)
    return player:getMark("rfenghou__dianyi-phase") == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:setPlayerMark(player, "rfenghou__dianyi-phase", 1)
    local targets = table.filter(room:getAlivePlayers(), function (p)
      return p:getHandcardNum() == player:getHandcardNum()
    end)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    if self.interaction.data == "draw1" then
      for _, p in ipairs(targets) do
        if not p.dead then
          p:drawCards(1, self.name)
        end
      end
    else
      local cards = {}
      for _, p in ipairs(targets) do
        if not p.dead and not p:isNude() and not player.dead then
          local card = room:askForCardsChosen(player, p, 1, 1, "he", self.name,
          "#rfenghou__dianyi-discard::"..p.id)
          if #card > 0 then
            room:throwCard(card, self.name, p, player)
            table.insertTableIfNeed(cards, card)
          end
        end
      end
      if player.dead then return end
      cards = table.filter(cards, function (id)
        return table.contains(room.discard_pile, id)
      end)
      if #cards == 0 then return end
      local equips = table.filter(cards, function (id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end)
      if #equips > 0 and (player:hasSkill(self, true) or player:hasSkill("rfenghou__chongshu", true)) then  --防止中途失去技能后一直挂在脸上
        player:addToPile(self.name, equips, true, self.name, player.id)
      end
      if player.dead then return end
      cards = table.filter(cards, function (id)
        return table.contains(room.discard_pile, id)
      end)
      if #cards > 0 then
        room:askForYiji(player, cards, nil, self.name, 0, 999, "#rfenghou__dianyi-give", cards)
      end
    end
  end,
}
local dianyi_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__dianyi_trigger",
  anim_type = "masochism",
  events = {fk.Damaged},
  main_skill = dianyi,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    local x = data.damage
    self.cancel_cost = false
    for i = 1, x do
      if self.cancel_cost or not player:hasSkill(self) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForUseActiveSkill(player, "rfenghou__dianyi", "#rfenghou__dianyi", true) then
      player:broadcastSkillInvoke("rfenghou__dianyi")
      return true
    end
    self.cancel_cost = true
  end,
}
local chongshu = fk.CreateTriggerSkill{
  name = "rfenghou__chongshu",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish and
      table.find(player.room.alive_players, function (p)
        return not table.contains(player:getTableMark(self.name), p.id) and
          table.find(player:getPile("rfenghou__dianyi"), function (id)
            local card = Fk:getCardById(id)
            return not p:prohibitUse(card) and not p:isProhibited(p, card)
          end)
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not table.contains(player:getTableMark(self.name), p.id) and
        table.find(player:getPile("rfenghou__dianyi"), function (id)
          return p:canUseTo(Fk:getCardById(id), p)
        end)
    end)
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#rfenghou__chongshu-choose", self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:addTableMark(player, self.name, to.id)
    while not player.dead and not to.dead do
      local cards = player:getPile("rfenghou__dianyi")
      if #cards == 0 then break end
      local use = U.askForUseRealCard(room, to, cards, nil, self.name,
      "#rfenghou__chongshu-use", {
        bypass_times = true,
        extraUse = true,
        expand_pile = cards,
      }, false, true)
      if use == nil then break end
    end
    if to.dead then return end
    if to:getHandcardNum() < #to:getCardIds("e") then
      to:drawCards(#to:getCardIds("e") - to:getHandcardNum(), self.name)
    end
    if to.dead then return end
    if to.hp < math.min(#to:getCardIds("e"), to.maxHp) then
      room:recover({
        who = to,
        num = math.min(#to:getCardIds("e"), to.maxHp) - to.hp,
        recoverBy = player,
        skillName = self.name,
      })
    end
  end,
}
dianyi:addRelatedSkill(dianyi_trigger)
murongyu:addSkill(dianyi)
murongyu:addSkill(chongshu)
Fk:loadTranslationTable{
  ["rfenghou__murongyu"] = "慕容虞",
  ["#rfenghou__murongyu"] = "嫡莲容姝",
  ["designer:rfenghou__murongyu"] = "yyuaN",
  ["illustrator:rfenghou__murongyu"] = "啪啪三国",
  ["cv:rfenghou__murongyu"] = "winsit",

  ["rfenghou__dianyi"] = "点漪",
  [":rfenghou__dianyi"] = "出牌阶段限一次，或当你受到1点伤害后，你可以令手牌数与你相同的角色各摸一张牌，或弃置手牌数与你相同的角色各一张牌，然后将弃置牌中的装备牌置于"..
  "你武将牌上，你可以分配其余的弃置牌。",
  ["rfenghou__chongshu"] = "宠殊",
  [":rfenghou__chongshu"] = "每名角色限一次，结束阶段，你可以令一名角色使用你武将牌上任意张牌，然后其摸牌和回复体力至其装备区牌数。",
  ["#rfenghou__dianyi"] = "点漪：令手牌数与你相同的角色各摸一张牌或弃一张牌",
  ["rfenghou__dianyi_discard"] = "弃一张牌",
  ["#rfenghou__dianyi-discard"] = "点漪：弃置 %dest 一张牌",
  ["#rfenghou__dianyi-give"] = "点漪：你可以分配这些牌",
  ["#rfenghou__dianyi_trigger"] = "点漪",
  ["#rfenghou__chongshu-choose"] = "宠殊：你可以令一名角色使用任意张“点漪”牌，其将手牌和体力值调整至其装备数",
  ["#rfenghou__chongshu-use"] = "宠殊：请使用其中一张装备",

  ["$rfenghou__dianyi1"] = "夕照楼台正桃夭，湿飞双翠破涟漪。",
  ["$rfenghou__dianyi2"] = "智者能调、达人自玉，绿窗何妨？红楼在星期。",
  ["$rfenghou__chongshu1"] = "亲卿爱卿，是以卿卿；我不卿卿，谁当卿卿？",
  ["$rfenghou__chongshu2"] = "君怀高天之志，妾当体夏夷之仪。",
  ["~rfenghou__murongyu"] = "青山待、长云爱，梦不到罗裙共袍带…",
}

local murongping = General:new(extension, "rfenghou__murongping", "yan", 4)
local xiaozhan = fk.CreateTriggerSkill{
  name = "rfenghou__xiaozhan",
  anim_type = "offensive",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card.trueName == "slash" then
      if event == fk.TargetSpecified then
        return data.firstTarget
      else
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local extra_data = {
      num = 2,
      min_num = 0,
      include_equip = true,
      skillName = self.name,
      pattern = ".",
    }
    local success, dat = player.room:askForUseActiveSkill(player, "discard_skill",
      "#rfenghou__xiaozhan-invoke:::"..data.card:toLogString(), true, extra_data)
    if success then
      self.cost_data = {cards = dat.cards or {}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    if #self.cost_data.cards == 2 then
      data.extra_data.rfenghou__xiaozhan = {player.id, 1}
      room:throwCard(self.cost_data.cards, self.name, player, player)
    else
      data.extra_data.rfenghou__xiaozhan = {player.id, 2}
      player:drawCards(2, self.name)
    end
    local use = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if use ~= nil then
      local new_card = Fk:cloneCard("unexpectation", data.card.suit, data.card.number)
      local c = table.simpleClone(data.card)
      for k, v in pairs(c) do
        if new_card[k] == nil then
          new_card[k] = v
        end
      end
      if data.card:isVirtual() then
        new_card.subcards = data.card.subcards
      else
        new_card.id = data.card.id
      end
      new_card.skillNames = data.card.skillNames
      new_card.skill = Fk.skills["unexpectation_skill"]
      data.card = new_card
      use.data[1].card = new_card
    end
  end,
}
local xiaozhan_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__xiaozhan_delay",
  mute = true,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.rfenghou__xiaozhan and data.extra_data.rfenghou__xiaozhan[1] == player.id and
      not player.dead and not data.damageDealt
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("rfenghou__xiaozhan")
    if data.extra_data.rfenghou__xiaozhan[2] == 1 then
      room:notifySkillInvoked(player, "rfenghou__xiaozhan", "drawcard")
      player:drawCards(2, "rfenghou__xiaozhan")
    else
      room:notifySkillInvoked(player, "rfenghou__xiaozhan", "negative")
      room:askForDiscard(player, 2, 2, true, "rfenghou__xiaozhan", false)
    end
  end,
}
xiaozhan:addRelatedSkill(xiaozhan_delay)
murongping:addSkill(xiaozhan)
AddWinAudio(murongping)
Fk:loadTranslationTable{
  ["rfenghou__murongping"] = "慕容评",
  ["#rfenghou__murongping"] = "三辅",
  ["designer:rfenghou__murongping"] = "屑",
  ["cv:rfenghou__murongping"] = "小叶子",
  ["illustrator:rfenghou__murongping"] = "啪啪三国",

  ["rfenghou__xiaozhan"] = "骁战",
  [":rfenghou__xiaozhan"] = "当你指定或成为【杀】的目标后，你可以摸/弃置两张牌，令此牌按【出其不意】结算。然后若此牌没有造成伤害，"..
  "你弃置/摸两张牌。",
  ["#rfenghou__xiaozhan-invoke"] = "骁战：是否将此%arg改为【出其不意】结算？（弃两张牌，或直接点“确定”摸两张牌）",

  ["$rfenghou__xiaozhan1"] = "出城浪战，以卵击石，吾破之必矣。",
  ["$rfenghou__xiaozhan2"] = "闭城留死，待断其手足，旋踵可灭。",
  ["~rfenghou__murongping"] = "千载一时……千载一时……",
  ["$rfenghou__murongping_win_audio"] = "太白经天，天下易主，此必然之验。",
}

local murongke = General:new(extension, "rfenghou__murongke", "yan", 4)
local qushi = fk.CreateTriggerSkill{
  name = "rfenghou__qushi",
  switch_skill_name = "rfenghou__qushi",
  anim_type = "drawcard",
  events = {fk.HpRecover},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
        return target ~= player
      else
        return target == player
      end
    end
  end,
  --[[on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#rfenghou__qushi-choose::"..target.id, self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,]]--
  on_use = function(self, event, target, player, data)
    local room = player.room
    --[[room:damage{
      from = target,
      to = room:getPlayerById(self.cost_data.tos[1]),
      damage = 1,
      skillName = self.name,
    }]]--
    player:drawCards(2, self.name)
  end,
}
local xujin = fk.CreateTriggerSkill{
  name = "rfenghou__xujin",
  anim_type = "control",
  events = {fk.Damage, fk.Damaged},
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) and #player:getTableMark("rfenghou__xujin-turn") < 3 then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event == nil then return end
      local current = turn_event.data[1]
      local n = 0
      room.logic:getActualDamageEvents(1, function (e)
        local damage = e.data[1]
        if damage.from == current then
          n = n + damage.damage
        end
      end, Player.HistoryTurn)
      room:setPlayerMark(player, "rfenghou__xujin-tmp", n)
      local choices = {}
      for _, choice in ipairs({"h", "e", "j"}) do
        if not table.contains(player:getTableMark("rfenghou__xujin-turn"), "rfenghou__xujin_"..choice) and
          table.find(room:getOtherPlayers(player), function (p)
            return table.find(room:getOtherPlayers(p), function (q)
              return math.abs(#p:getCardIds(choice) - #q:getCardIds(choice)) <= n
            end)
          end) then
          table.insert(choices, "rfenghou__xujin_"..choice)
        end
      end
      if #choices > 0 then
        room:setPlayerMark(player, "rfenghou__xujin-tmp", {choices, n})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local success, dat = room:askForUseActiveSkill(player, "rfenghou__xujin_active",
      "#rfenghou__xujin-invoke:::"..player:getMark("rfenghou__xujin-tmp")[2], true, nil, false)
    room:setPlayerMark(player, "rfenghou__xujin-tmp", 0)
    if success and dat then
      room:sortPlayersByAction(dat.targets)
      self.cost_data = {tos = dat.targets, choice = dat.interaction}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = table.map(self.cost_data.tos, Util.Id2PlayerMapper)
    local choice = self.cost_data.choice[17]
    room:addTableMark(player, "rfenghou__xujin-turn", choice)
    local mapper = {
      ["h"] = Card.PlayerHand,
      ["e"] = Card.PlayerEquip,
      ["j"] = Card.PlayerJudge,
    }
    U.swapCards(room, player, targets[1], targets[2], targets[1]:getCardIds(choice), targets[2]:getCardIds(choice), self.name,
      mapper[choice])
  end,
}
local xujin_active = fk.CreateActiveSkill{
  name = "rfenghou__xujin_active",
  card_num = 0,
  target_num = 2,
  interaction = function(self)
    local choices = Self:getTableMark("rfenghou__xujin-tmp")[1]
    return UI.ComboBox { choices = choices, all_choices = {"rfenghou__xujin_h", "rfenghou__xujin_e", "rfenghou__xujin_j"} }
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    if #selected < 2 and to_select ~= Self.id then
      if #selected == 0 then
        return true
      else
        local target1 = Fk:currentRoom():getPlayerById(selected[1])
        local target2 = Fk:currentRoom():getPlayerById(to_select)
        local area = self.interaction.data[17]
        local n = Self:getTableMark("rfenghou__xujin-tmp")[2]
        return math.abs(#target1:getCardIds(area) - #target2:getCardIds(area)) <= n and
          not (#target1:getCardIds(area) == 0 and #target2:getCardIds(area) == 0)
      end
    end
  end,
}
Fk:addSkill(xujin_active)
murongke:addSkill(qushi)
murongke:addSkill(xujin)
Fk:loadTranslationTable{
  ["rfenghou__murongke"] = "慕容恪",
  ["#rfenghou__murongke"] = "古之遗爱",
  ["cv:rfenghou__murongke"] = "幽蝶化烬",
  ["designer:rfenghou__murongke"] = "扬林",
  ["illustrator:rfenghou__murongke"] = "啪啪三国",

  ["rfenghou__qushi"] = "驱势",
  [":rfenghou__qushi"] = "转换技，①其他角色②你回复体力后，你可以摸两张牌。",
  ["rfenghou__xujin"] = "虚襟",
  [":rfenghou__xujin"] = "你造成或受到伤害后，你可以选择此回合未选择过一项，令数值相差不大于X（X为当前回合角色此回合造成的伤害值）的两名其他角色"..
  "交换对应值：1.手牌数；2.装备区牌数；3.判定区牌数。",
  --["#rfenghou__qushi-choose"] = "驱势：你可以令 %dest 对一名角色造成1点伤害",
  ["#rfenghou__xujin-invoke"] = "虚襟：你可以令两名对应项差值不大于%arg的角色交换对应值",
  ["rfenghou__xujin_active"] = "虚襟",
  ["rfenghou__xujin_h"] = "交换手牌",
  ["rfenghou__xujin_e"] = "交换装备",
  ["rfenghou__xujin_j"] = "交换判定区",

  ["$rfenghou__qushi1"] = "老贼计穷势迫，卒谋叛逆，困则自毙。",
  ["$rfenghou__qushi2"] = "群羊虽众，匹夫持一竿可驱之。",
  ["$rfenghou__xujin1"] = "虚心静志，去喜去恶。从室视庭，咫尺已具。",
  ["$rfenghou__xujin2"] = "赏者赏，刑者刑，因其所为，各以自成。",
  ["~rfenghou__murongke"] = "雄心醉中老，唯余樽空虑。",
}

local murongfu = General:new(extension, "rfenghou__murongfu", "yan", 4)
local bidao = fk.CreateTriggerSkill{
  name = "rfenghou__bidao",
  anim_type = "drawcard",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.tos and table.contains(TargetGroup:getRealTargets(data.tos), player.id) and
      player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(data.card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
    if player.dead then return end
    local cards = table.filter(Card:getIdList(data.card), function (id)
      return table.contains(player:getCardIds("h"), id)
    end)
    if #cards > 0 then
      DIY.ShowCards(player, cards)
    end
    if data.card.suit ~= Card.NoSuit and
      not (data.extra_data and data.extra_data.rfenghou__bidao_targets and
      table.contains(data.extra_data.rfenghou__bidao_targets, player.id)) and
      not player.dead and not player:isNude() then
      room:askForDiscard(player, 1, 1, true, self.name, false, ".|.|"..data.card:getSuitString(),
        "#rfenghou__bidao-discard:::"..data.card:getSuitString(true))
    end
  end,

  refresh_events = {fk.CardUseFinished, fk.CardRespondFinished, fk.PreCardUse},
  can_refresh = function(self, event, target, player, data)
    if target == player then
      if event == fk.CardUseFinished or event == fk.CardRespondFinished then
        return player:hasSkill(self, true) and data.responseToEvent and data.responseToEvent.card
      elseif event == fk.PreCardUse then
        return player:hasSkill(self) and table.contains(DIY.GetShownCards(player), data.card.id) and
          data.tos and table.find(TargetGroup:getRealTargets(data.tos), function (id)
            local p = player.room:getPlayerById(id)
            return p:getHandcardNum() == player:getHandcardNum() and p.hp == player.hp
          end)
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.CardUseFinished or event == fk.CardRespondFinished then
      local use_event = nil
      player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        if use.from == data.responseToEvent.from and use.card == data.responseToEvent.card then
          use_event = e
          return true
        end
      end, 1)
      if use_event == nil then return end
      local use = use_event.data[1]
      if use.tos and table.contains(TargetGroup:getRealTargets(use.tos), player.id) then
        use.extra_data = use.extra_data or {}
        local record = use.extra_data.rfenghou__bidao_targets or {}
        table.insert(record, player.id)
        use.extra_data.rfenghou__bidao_targets = record
      end
    elseif event == fk.PreCardUse then
      data.extraUse = true
    end
  end,
}
local bidao_prohibit = fk.CreateProhibitSkill{
  name = "#rfenghou__bidao_prohibit",
  main_skill = bidao,
  is_prohibited = function(self, from, to, card)
    return from:hasSkill(bidao) and table.contains(DIY.GetShownCards(from), card.id) and from:getHandcardNum() ~= to:getHandcardNum()
  end,
}
local pitu = fk.CreateActiveSkill{
  name = "rfenghou__pitu",
  anim_type = "control",
  card_num = 1,
  target_num = 1,
  prompt = "#rfenghou__pitu",
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  interaction = UI.ComboBox { choices = {"rfenghou__pitu1", "rfenghou__pitu2", "rfenghou__pitu3"} },
  card_filter = function(self, to_select, selected)
    return #selected == 0 and table.contains(Self:getCardIds("h"), to_select)
  end,
  target_filter = function(self, to_select, selected, selected_cards, _, _, player)
    if #selected == 0 and #selected_cards == 1 then
      local card = Fk:getCardById(selected_cards[1])
      local target = Fk:currentRoom():getPlayerById(to_select)
      if self.interaction.data == "rfenghou__pitu2" and target:isNude() then return end
      if not player:canUse(card) or not player:canUseTo(card, target) or card.is_passive then
        return true
      end
      if to_select == Self.id then
        if card.skill:getMinTargetNum() == 0 and not card.multiple_targets and player:canUse(card) then return end
      end
      if card.multiple_targets then return end
      return not card.skill:targetFilter(to_select, {}, selected_cards, card, nil, player)
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    player:showCards(effect.cards)
    if self.interaction.data == "rfenghou__pitu1" then
      if not player.dead then
        player:drawCards(1, self.name)
      end
      if player.dead then return end
      if not (target.dead or player:isNude() or target == player) then
        local card = room:askForCard(player, 1, 1, true, self.name, false, nil, "#rfenghou__pitu-give::"..target.id)
        room:moveCardTo(card, Card.PlayerHand, target, fk.ReasonGive, self.name, nil, false, player.id)
      end
    elseif self.interaction.data == "rfenghou__pitu2" then
      if player.dead then return end
      if not (target.dead or target:isNude()) then
        if target == player then
          room:askForDiscard(player, 1, 1, true, self.name, false, nil, "#rfenghou__pitu-discard::"..target.id)
        else
          local card = room:askForCardChosen(player, target, "he", self.name, "#rfenghou__pitu-discard::"..target.id)
          room:throwCard(card, self.name, target, player)
        end
      end
    elseif self.interaction.data == "rfenghou__pitu3" then
      if not target.dead then
        room:damage{
          from = player,
          to = target,
          damage = 1,
          skillName = self.name,
        }
      end
    end
    if player.dead then return end
    local card = Fk:getCardById(effect.cards[1])
    if target.dead or not table.contains(player:getCardIds("h"), effect.cards[1]) or
      self:targetFilter(target.id, {}, effect.cards, nil, nil, player) or
      not U.askForUseRealCard(room, player, effect.cards, nil, self.name,
      "#rfenghou__pitu-use::"..target.id..":"..card:toLogString(), {exclusive_targets = {target.id}}) then
      player._phase_end = true
      return
    end
  end,
}
bidao:addRelatedSkill(bidao_prohibit)
murongfu:addSkill(bidao)
murongfu:addSkill(pitu)
Fk:loadTranslationTable{
  ["rfenghou__murongfu"] = "慕容复",
  ["#rfenghou__murongfu"] = "",
  ["designer:rfenghou__murongfu"] = "低调的温柔",
  ["illustrator:rfenghou__murongfu"] = "谋定天下",

  ["rfenghou__bidao"] = "彼道",
  [":rfenghou__bidao"] = "以你为目标的牌结算后，你可以获得并明置之，若你未使用或打出牌响应此牌，你需弃置一张同花色牌。你使用明置牌只能指定手牌数"..
  "等于你的角色为目标，若体力值也相等，则不计次数。",
  ["rfenghou__pitu"] = "辟途",  --傻逼技能
  [":rfenghou__pitu"] = "出牌阶段，你可以展示一张牌，对一名不为此牌（于出牌阶段空闲时机使用时的）合法目标的角色执行一项："..
  "1.摸一张牌并交给其一张牌；2.弃置其一张牌；3.对其造成1点伤害。然后你对其使用此牌（有出牌阶段空闲时机使用合法性限制）或结束此阶段。",
  ["#rfenghou__bidao-discard"] = "彼道：请弃置一张%arg牌",
  ["#rfenghou__pitu"] = "辟途：展示一张手牌，对一名此牌不能指定的角色执行一项",
  ["rfenghou__pitu1"] = "摸一张牌并交给其一张牌",
  ["rfenghou__pitu2"] = "弃置其一张牌",
  ["rfenghou__pitu3"] = "对其造成1点伤害",
  ["#rfenghou__pitu-give"] = "辟途：请交给 %dest 一张牌",
  ["#rfenghou__pitu-discard"] = "辟途：弃置 %dest 一张牌",
  ["#rfenghou__pitu-use"] = "辟途：对 %dest 使用%arg，或点“取消”结束出牌阶段！",
}

return extension
